package cn.jingyuan.swan.system.web.controller.gateway;

import cn.jingyuan.bee.utils.bean.BeanUtils;
import cn.jingyuan.swan.cloud.core.RestResult;
import cn.jingyuan.swan.cloud.core.RestResultUtils;
import cn.jingyuan.swan.system.domain.gateway.entity.GatewayRoute;
import cn.jingyuan.swan.system.model.dto.GatewayRouteDto;
import cn.jingyuan.swan.system.service.GatewayRouteService;
import cn.jingyuan.swan.system.web.restful.GatewayRouteRestful;
import cn.jingyuan.swan.system.web.restful.model.GatewayRouteAddRequest;
import cn.jingyuan.swan.system.web.restful.model.GatewayRouteUpdateRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "网关路由接口")
@Slf4j
@RestController
public class GatewayRouteController implements GatewayRouteRestful {

    @Resource
    GatewayRouteService gatewayRouteService;

    @ApiOperation(value = "添加路由", notes = "添加路由")
    @Override
    public RestResult<Long> addRoute(GatewayRouteAddRequest reqDto) {
        GatewayRoute add = new GatewayRoute();

        BeanUtils.copyProperties(reqDto, add);

        Long routeId = gatewayRouteService.addRoute(add);

        return RestResultUtils.ok(routeId);
    }

    @ApiOperation(value = "移除路由", notes = "移除路由")
    @Override
    public RestResult<Void> removeRoute(List<Long> ids) {
        gatewayRouteService.removeRoute(ids);
        return null;
    }

    @ApiOperation(value = "更新路由", notes = "更新路由")
    @Override
    public RestResult<Void> updateRoute(GatewayRouteUpdateRequest reqDto) {
        return null;
    }

    @ApiOperation(value = "获取路由列表", notes = "获取路由列表")
    @Override
    public RestResult<List<GatewayRouteDto>> getRoutes() {
        List<GatewayRoute> routes = gatewayRouteService.getRoutes();
        List<GatewayRouteDto> list = new ArrayList<>(routes.size());
        for (GatewayRoute route : routes) {
            GatewayRouteDto dto = new GatewayRouteDto();
            BeanUtils.copyProperties(route, dto);
            list.add(dto);
        }
        return RestResultUtils.ok(list);
    }

}
